package com.zhny.aiot.adjust.valve.plugin;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhny.aiot.adjust.valve.plugin.model.AdjustValveStoreEntry;
import com.zhny.aiot.adjust.valve.plugin.model.AdjustableParam;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDevice;
import com.zhny.aiot.plugin.core.utils.ConfigUtils;
import com.zhny.aiot.plugin.model.CommandEntry;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.FactoryType;
import com.zhny.aiot.plugin.model.StoreEntity;
import com.zhny.aiot.plugin.zhnygateway.GatewayServer;
import com.zhny.aiot.plugin.zhnygateway.device.ZhnyDeviceFactory;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

import java.util.List;

public class AdjustableValvePlugin extends Plugin {
    public AdjustableValvePlugin(PluginWrapper wrapper) {
        super (wrapper);
    }

    @Extension
    public static class AdjustableValveFactory extends ZhnyDeviceFactory<AdjustValveStoreEntry> {

        @Override
        public List<CommandEntry> supportCommandClasses() {
            String paramJson;
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            try {
                paramJson = mapper.writeValueAsString(ConfigUtils.getConfigFields(AdjustableParam.class));
            } catch (Exception e) {
                return null;
            }
            List<CommandEntry> commands = super.supportCommandClasses ();
            commands.add(CommandEntry.valueOf("openValve1", "A阀门","有参数", paramJson));
            commands.add(CommandEntry.valueOf("openValve2", "B阀门","有参数", paramJson));
            commands.add(CommandEntry.valueOf("openValve3", "C阀门","有参数" ,paramJson));
            commands.add(CommandEntry.valueOf("openValve4", "D阀门", "有参数",paramJson));
            commands.add(CommandEntry.valueOf("queryStatus", "查询阀门状态", "无参数",null));
            commands.add(CommandEntry.valueOf("queryVoltage", "查询阀门电压", "无参数",null));
            return commands;
        }

        public IDevice createDevice(ICenterFacilities iCenterFacilities, DeviceEntity entity) {
            return new AdjustableDevice (GatewayServer.getInstance ().getEngine () ,this,iCenterFacilities,entity);
        }

        public Class<? extends StoreEntity> storeClass() {
            return AdjustValveStoreEntry.class;
        }

        public String getId() {
            return "zhny-adjustable-valve-plugin";
        }

        public String getName() {
            return "智慧可调阀门插件";
        }

        public FactoryType getType() {
            return FactoryType.ValveController;
        }


    }
}
